package com.example.userlogin.controller;


import com.example.userlogin.entity.Goods;
import com.example.userlogin.service.GoodsService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/goods")
//专门处理商品控制器
public class GoodsController {
//    实现与前端交互的数据请求方法
//    1.进入模块首页的时候需要对数据里的所有商品数据进行查询，返回列表给前端页面进行展示
    @Autowired
    GoodsService goodsService;

    @Autowired
    HttpServletRequest request;

//    1.展示数据库里已有的商品列表给前端
    @RequestMapping("/goodsList")
    public String GoodsMain(Model model){
//        调用service方法查询到商品列表结果
        List<Goods> goodsList = goodsService.findAll();
        Object username = request.getSession().getAttribute("username");
//        将查询结果用model绑定
        model.addAttribute("data",goodsList);
        model.addAttribute("username",username);
        System.out.println(goodsList);
//        返回视图
        return "goods/goodsList";
    }

    @RequestMapping("/goodsAdd")
    public String goodsAdd(Model model){
        Object username = request.getSession().getAttribute("username");
        model.addAttribute("username",username);
        return "goods/goodsAdd";
    }

    @RequestMapping("/goodsAddProcess")
    public String goodsAddProcess(@RequestParam String name,@RequestParam double price
            ,@RequestParam Integer number,@RequestParam String category,
                                  @RequestParam String supplier){
        //      组装一个goods实体对象
        Goods  goods = new Goods();
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setNumber(Integer.valueOf(number));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        //        调用service的add方法给数据库
        if (goodsService.add(goods)==1){
        //        如果返回ok，那么就进入商品管理首页
        return "redirect:/goods/goodsList";
        }else {
            return "redirect:/goods/goodsList";
        }
    }

    @RequestMapping("/goodsUpdate")
    public String update(@RequestParam String id,Model model){
//        首先基于id查询出来该id记录里的所有数据信息
        Goods goods=new Goods();
        goods.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
        Goods goodDB = goodsService.findOne(goods);
//        2.将这些数据信息返回给视图
        model.addAttribute("data",goodDB);
        Object username = request.getSession().getAttribute("username");
        model.addAttribute("username",username);
//        3.返回视图文件
        return "goods/goodsUpdate";
    }

    @RequestMapping("/goodsUpdateProcess")
    public String goodsUpdateProcess(@RequestParam String name,@RequestParam double price
            ,@RequestParam Integer number,@RequestParam String category,
                                     @RequestParam String supplier){
        //      组装一个goods实体对象
        Goods  goods = new Goods();
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setNumber(Integer.valueOf(number));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        //        调用service的add方法给数据库
        if (goodsService.add(goods)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/goods";
        }else {
            return "redirect:/goods";
        }
    }
}
